const MyPromise = require("../template.js")

/** 十四、Promise.any 测试 */

// 测试 1: 第一个成功的 Promise
function testAnyFirstFulfilled() {
  const p1 = MyPromise.reject("Fail 1")
  const p2 = MyPromise.resolve(2)
  const p3 = MyPromise.resolve(3)

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.1 First fulfilled:", value === 2),
    (reason) => console.log("14.1 First fulfilled:", false)
  )
}

// 测试 2: 异步第一个成功
function testAnyAsyncFirst() {
  const p1 = new MyPromise((resolve) => setTimeout(() => resolve(1), 100))
  const p2 = new MyPromise((resolve) => setTimeout(() => resolve(2), 50))
  const p3 = new MyPromise((resolve) => setTimeout(() => resolve(3), 150))

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.2 Async first:", value === 2), // 最快的是 p2
    (reason) => console.log("14.2 Async first:", false)
  )
}

// 测试 3: 所有 Promise 都失败
function testAnyAllRejected() {
  const p1 = MyPromise.reject("Error 1")
  const p2 = MyPromise.reject("Error 2")
  const p3 = MyPromise.reject("Error 3")

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.3 All rejected:", false),
    (error) => {
      // 应该抛出 AggregateError
      console.log("14.3 All rejected - AggregateError:", error.name === "AggregateError")
      console.log("14.4 All rejected - errors length:", error.errors.length === 3)
      console.log("14.5 All rejected - first error:", error.errors[0] === "Error 1")
    }
  )
}

// 测试 4: 包含普通值
function testAnyWithNonPromise() {
  const p1 = MyPromise.reject("Fail")
  const p2 = 2 // 普通值，立即成功
  const p3 = MyPromise.resolve(3)

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.6 Non-promise value:", value === 2),
    (reason) => console.log("14.6 Non-promise value:", false)
  )
}

// 测试 5: 混合失败和成功，不同延迟
function testAnyMixedAsync() {
  const p1 = new MyPromise((_, reject) => setTimeout(() => reject("Fast Fail"), 30))
  const p2 = new MyPromise((_, reject) => setTimeout(() => reject("Slow Fail"), 100))
  const p3 = new MyPromise((resolve) => setTimeout(() => resolve(3), 80))

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.7 Mixed async - 第一个成功:", value === 3),
    (reason) => console.log("14.7 Mixed async:", false)
  )
}

// 测试 6: 空数组（应该立即失败）
function testAnyEmpty() {
  MyPromise.any([]).then(
    (value) => console.log("14.8 Empty array:", false),
    (error) => {
      console.log("14.8 Empty array - AggregateError:", error.name === "AggregateError")
      console.log("14.9 Empty array - no errors:", error.errors.length === 0)
    }
  )
}

// 测试 7: 第一个是成功的，后面的都失败
function testAnyFirstSuccess() {
  const p1 = MyPromise.resolve(1)
  const p2 = MyPromise.reject("Fail 2")
  const p3 = MyPromise.reject("Fail 3")

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.10 First is success:", value === 1),
    (reason) => console.log("14.10 First is success:", false)
  )
}

// 测试 8: 最后一个成功
function testAnyLastSuccess() {
  const p1 = new MyPromise((_, reject) => setTimeout(() => reject("Fail 1"), 50))
  const p2 = new MyPromise((_, reject) => setTimeout(() => reject("Fail 2"), 80))
  const p3 = new MyPromise((resolve) => setTimeout(() => resolve(3), 100))

  MyPromise.any([p1, p2, p3]).then(
    (value) => console.log("14.11 Last success:", value === 3),
    (reason) => console.log("14.11 Last success:", false)
  )
}

testAnyFirstFulfilled()
testAnyAsyncFirst()
testAnyAllRejected()
testAnyWithNonPromise()
testAnyMixedAsync()
testAnyEmpty()
testAnyFirstSuccess()
testAnyLastSuccess()

